$(() => {
    template.defaults.imports.moment = moment

    const params = location.search.slice(1).split('=')[1]
    //存储数据
    let data = {}
    //渲染文章详情函数
    function renderArticle() {
        const html = template('detailTPL', data)
        $('#detailBox').html(html)
    }

    $.ajax({
        type: 'get',
        url: `/posts/${params}`,
        success(res) {
            data = res
            renderArticle()
        },
        error() {
            alert('获取文章数据失败!')
        }
    })

    $('#detailBox').on('click', '#like', function () {
        $.ajax({
            type: 'post',
            url: `/posts/fabulous/${params}`,
            success(res) {
                alert('点赞成功!')
                Object.assign(data, res)
                renderArticle()
            }
        })

    })
})